import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class AttachmentService extends BaseService<any> {
  private readonly logger = new Logger(AttachmentService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * add
   * 对应 PHP: AttachmentService_admin::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: $data[ 'site_id' ] = $this->site_id;         return $this->core_attachment_service->add($data);     }      /**      *      * /**      * 编辑素材      * @p...
data[ 'site_id' ] = this.site_id;
        return this.core_attachment_service.add(data);
    }

    /**
     *
     * /**
     * 编辑素材
     * @param int att_id
     * @param data
     * @return SysAttachment
     */
    async edit(int att_id, data)
    {
        return this.core_attachment_service.edit(this.site_id, att_id, data);
    }

    /**
     * 修改附件分组
     * @param att_id
     * @param cate_id
     * @return bool
     */
    async modifyCategory(att_id, cate_id)
    {
        const where = [
            [ 'att_id', '=', att_id ],
            [ 'site_id', '=', this.site_id ),
        ];
        this.model.where(where).update([ cate_id: cate_id, update_time: time() ]];
        return true;
    }

    /**
     * 批量更新附件分组
     * @param att_ids
     * @param cate_id
     * @return bool
     */
    async batchModifyCategory(att_ids, cate_id)
    {

        const where = [
            [ 'att_id', 'in', typeof att_ids) ? explode(att_ids) : att_ids ],
            [ 'site_id', '=', this.site_id ],
        ];
        this.model.where(where).update([ cate_id: cate_id, update_time: time() ]];
        return true;
    }

    /**
     * 删除素材
     * @param int att_id
     * @return mixed
     */
    async del(int att_id)
    {
        return this.core_attachment_service.del(this.site_id, att_id);
    }

    /**
     * 批量删除
     * @param data
     * @return true|null
     */
    async delAll(data)
    {
        return this.core_attachment_service.delAll(this.site_id, data);
    }

    /**
     * 管理端获取附件列表
     * @param data
     * @return */
    async getPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'att_type' ))) {
            where[] = [ 'att_type', '=', data[ 'att_type' ] ];
  }

  /**
   * edit
   * 对应 PHP: AttachmentService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(att_id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: return $this->core_attachment_service->edit($this->site_id, $att_id, $data);     }      /**      * 修改附件分组      * @param $att_id      * @param $cate_id...
return this.core_attachment_service.edit(this.site_id, att_id, data);
    }

    /**
     * 修改附件分组
     * @param att_id
     * @param cate_id
     * @return bool
     */
    async modifyCategory(att_id, cate_id)
    {
        const where = [
            [ 'att_id', '=', att_id ],
            [ 'site_id', '=', this.site_id ),
        ];
        this.model.where(where).update([ cate_id: cate_id, update_time: time() ]];
        return true;
    }

    /**
     * 批量更新附件分组
     * @param att_ids
     * @param cate_id
     * @return bool
     */
    async batchModifyCategory(att_ids, cate_id)
    {

        const where = [
            [ 'att_id', 'in', typeof att_ids) ? explode(att_ids) : att_ids ],
            [ 'site_id', '=', this.site_id ],
        ];
        this.model.where(where).update([ cate_id: cate_id, update_time: time() ]];
        return true;
    }

    /**
     * 删除素材
     * @param int att_id
     * @return mixed
     */
    async del(int att_id)
    {
        return this.core_attachment_service.del(this.site_id, att_id);
    }

    /**
     * 批量删除
     * @param data
     * @return true|null
     */
    async delAll(data)
    {
        return this.core_attachment_service.delAll(this.site_id, data);
    }

    /**
     * 管理端获取附件列表
     * @param data
     * @return */
    async getPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'att_type' ))) {
            where[] = [ 'att_type', '=', data[ 'att_type' ] ];
  }

  /**
   * modifyCategory
   * 对应 PHP: AttachmentService_admin::modifyCategory()
   * 逻辑类型: undefined - undefined
   */
  async modifyCategory(att_id: any, cate_id: any) {
    // 基于PHP真实逻辑: modifyCategory
    // PHP原文: $where = array(             [ 'att_id', '=', $att_id ],             [ 'site_id', '=', $this->site_id ],         );         $this->model->where($where)...
const where = [
            [ 'att_id', '=', att_id ],
            [ 'site_id', '=', this.site_id ),
        ];
        this.model.where(where).update([ cate_id: cate_id, update_time: time() ]];
        return true;
    }

    /**
     * 批量更新附件分组
     * @param att_ids
     * @param cate_id
     * @return bool
     */
    async batchModifyCategory(att_ids, cate_id)
    {

        const where = [
            [ 'att_id', 'in', typeof att_ids) ? explode(att_ids) : att_ids ],
            [ 'site_id', '=', this.site_id ],
        ];
        this.model.where(where).update([ cate_id: cate_id, update_time: time() ]];
        return true;
    }

    /**
     * 删除素材
     * @param int att_id
     * @return mixed
     */
    async del(int att_id)
    {
        return this.core_attachment_service.del(this.site_id, att_id);
    }

    /**
     * 批量删除
     * @param data
     * @return true|null
     */
    async delAll(data)
    {
        return this.core_attachment_service.delAll(this.site_id, data);
    }

    /**
     * 管理端获取附件列表
     * @param data
     * @return */
    async getPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'att_type' ))) {
            where[] = [ 'att_type', '=', data[ 'att_type' ] ];
  }

  /**
   * batchModifyCategory
   * 对应 PHP: AttachmentService_admin::batchModifyCategory()
   * 逻辑类型: undefined - undefined
   */
  async batchModifyCategory(att_ids: any, cate_id: any) {
    // 基于PHP真实逻辑: batchModifyCategory
    // PHP原文: $where = array(             [ 'att_id', 'in', is_string($att_ids) ? explode($att_ids) : $att_ids ],             [ 'site_id', '=', $this->site_id ],   ...
const where = [
            [ 'att_id', 'in', typeof att_ids) ? explode(att_ids) : att_ids ],
            [ 'site_id', '=', this.site_id ],
        ];
        this.model.where(where).update([ cate_id: cate_id, update_time: time() ]];
        return true;
    }

    /**
     * 删除素材
     * @param int att_id
     * @return mixed
     */
    async del(int att_id)
    {
        return this.core_attachment_service.del(this.site_id, att_id);
    }

    /**
     * 批量删除
     * @param data
     * @return true|null
     */
    async delAll(data)
    {
        return this.core_attachment_service.delAll(this.site_id, data);
    }

    /**
     * 管理端获取附件列表
     * @param data
     * @return */
    async getPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'att_type' ))) {
            where[] = [ 'att_type', '=', data[ 'att_type' ] ];
  }

  /**
   * del
   * 对应 PHP: AttachmentService_admin::del()
   * 逻辑类型: undefined - undefined
   */
  async del(att_id: number) {
    // 基于PHP真实逻辑: del
    // PHP原文: return $this->core_attachment_service->del($this->site_id, $att_id);     }      /**      * 批量删除      * @param $data      * @return true|null      */  ...
return this.core_attachment_service.del(this.site_id, att_id);
    }

    /**
     * 批量删除
     * @param data
     * @return true|null
     */
    async delAll(data)
    {
        return this.core_attachment_service.delAll(this.site_id, data);
    }

    /**
     * 管理端获取附件列表
     * @param data
     * @return */
    async getPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'att_type' ))) {
            where[] = [ 'att_type', '=', data[ 'att_type' ] ];
  }

  /**
   * delAll
   * 对应 PHP: AttachmentService_admin::delAll()
   * 逻辑类型: undefined - undefined
   */
  async delAll(data: any) {
    // 基于PHP真实逻辑: delAll
    // PHP原文: return $this->core_attachment_service->delAll($this->site_id, $data);     }      /**      * 管理端获取附件列表      * @param array $data      * @return array  ...
return this.core_attachment_service.delAll(this.site_id, data);
    }

    /**
     * 管理端获取附件列表
     * @param data
     * @return */
    async getPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'att_type' ))) {
            where[] = [ 'att_type', '=', data[ 'att_type' ] ];
  }

  /**
   * getPage
   * 对应 PHP: AttachmentService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(data: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $where = array(             [ 'site_id', '=', $this->site_id ]         );         if (!empty($data[ 'att_type' ])) {             $where[] = [ 'att_typ...
const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'att_type' ))) {
            where[] = [ 'att_type', '=', data[ 'att_type' ] ];
  }

  /**
   * addCategory
   * 对应 PHP: AttachmentService_admin::addCategory()
   * 逻辑类型: undefined - undefined
   */
  async addCategory(data: any[]) {
    // 基于PHP真实逻辑: addCategory
    // PHP原文: $data[ 'site_id' ] = $this->site_id;         $category_model = new SysAttachmentCategory();         $attachment = $category_model->create($data);     ...
data[ 'site_id' ] = this.site_id;
        const category_model = this.sysAttachmentCategoryService;
        const attachment = category_model.create(data);
        if (!attachment.id)
            throw new AdminBusinessException('ADD_FAIL');//创建失败
        return attachment.id;
    }

    /**
     * 素材组模型对象
     * @param int site_id
     * @param int id
     * @return mixed
     */
    async findCategory(int site_id, int id)
    {
        const where = [
            [ 'site_id', '=', site_id ],
            [ 'id', '=', id )
        ];
        const category_model = this.sysAttachmentCategoryService;
        const category = category_model.where(where).findOrEmpty(];
        if (category.isEmpty())
            throw new AdminBusinessException('ATTACHMENT_GROUP_NOT_EXIST');
        return category;
    }

    /**
     * 编辑素材组
     * @param int id
     * @param data
     * @return SysAttachmentCategory
     */
    async editCategory(int id, data)
    {
        const where = [
            [ 'site_id', '=', this.site_id ],
            [ 'id', '=', id )
        ];
        const category_model = this.sysAttachmentCategoryService;
        return category_model.where(where).update(data);
    }

    /**
     * 删除素材组
     * @param int id
     * @return mixed
     * @throws DbBusinessException
     */
    async delCategory(int id)
    {

        //查询是否有下级菜单或按钮
        const category = this.findCategory(this.site_id, id];
        if (this.model.where([ [ 'cate_id', '=', id ) )).count() > 0)
            throw new AdminBusinessException('ATTACHMENT_GROUP_HAS_IMAGE'];

        //下级存在图片不能删除
        return category.delete();

    }

    /**
     * 管理端获取附件组列表
     * @param data
     * @return * @throws DbBusinessException
     */
    async getCategoryPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'type' ))) {
            where[] = [ 'type', '=', data[ 'type' ] ];
  }

  /**
   * findCategory
   * 对应 PHP: AttachmentService_admin::findCategory()
   * 逻辑类型: undefined - undefined
   */
  async findCategory(site_id: number, id: number) {
    // 基于PHP真实逻辑: findCategory
    // PHP原文: $where = array(             [ 'site_id', '=', $site_id ],             [ 'id', '=', $id ]         );         $category_model = new SysAttachmentCategor...
const where = [
            [ 'site_id', '=', site_id ],
            [ 'id', '=', id )
        ];
        const category_model = this.sysAttachmentCategoryService;
        const category = category_model.where(where).findOrEmpty(];
        if (category.isEmpty())
            throw new AdminBusinessException('ATTACHMENT_GROUP_NOT_EXIST');
        return category;
    }

    /**
     * 编辑素材组
     * @param int id
     * @param data
     * @return SysAttachmentCategory
     */
    async editCategory(int id, data)
    {
        const where = [
            [ 'site_id', '=', this.site_id ],
            [ 'id', '=', id )
        ];
        const category_model = this.sysAttachmentCategoryService;
        return category_model.where(where).update(data);
    }

    /**
     * 删除素材组
     * @param int id
     * @return mixed
     * @throws DbBusinessException
     */
    async delCategory(int id)
    {

        //查询是否有下级菜单或按钮
        const category = this.findCategory(this.site_id, id];
        if (this.model.where([ [ 'cate_id', '=', id ) )).count() > 0)
            throw new AdminBusinessException('ATTACHMENT_GROUP_HAS_IMAGE'];

        //下级存在图片不能删除
        return category.delete();

    }

    /**
     * 管理端获取附件组列表
     * @param data
     * @return * @throws DbBusinessException
     */
    async getCategoryPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'type' ))) {
            where[] = [ 'type', '=', data[ 'type' ] ];
  }

  /**
   * editCategory
   * 对应 PHP: AttachmentService_admin::editCategory()
   * 逻辑类型: undefined - undefined
   */
  async editCategory(id: any[], data: any[]) {
    // 基于PHP真实逻辑: editCategory
    // PHP原文: $where = array(             [ 'site_id', '=', $this->site_id ],             [ 'id', '=', $id ]         );         $category_model = new SysAttachmentC...
const where = [
            [ 'site_id', '=', this.site_id ],
            [ 'id', '=', id )
        ];
        const category_model = this.sysAttachmentCategoryService;
        return category_model.where(where).update(data);
    }

    /**
     * 删除素材组
     * @param int id
     * @return mixed
     * @throws DbBusinessException
     */
    async delCategory(int id)
    {

        //查询是否有下级菜单或按钮
        const category = this.findCategory(this.site_id, id];
        if (this.model.where([ [ 'cate_id', '=', id ) )).count() > 0)
            throw new AdminBusinessException('ATTACHMENT_GROUP_HAS_IMAGE'];

        //下级存在图片不能删除
        return category.delete();

    }

    /**
     * 管理端获取附件组列表
     * @param data
     * @return * @throws DbBusinessException
     */
    async getCategoryPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'type' ))) {
            where[] = [ 'type', '=', data[ 'type' ] ];
  }

  /**
   * delCategory
   * 对应 PHP: AttachmentService_admin::delCategory()
   * 逻辑类型: undefined - undefined
   */
  async delCategory(id: number) {
    // 基于PHP真实逻辑: delCategory
    // PHP原文: //查询是否有下级菜单或按钮         $category = $this->findCategory($this->site_id, $id);         if ($this->model->where([ [ 'cate_id', '=', $id ] ])->count() > 0...
//查询是否有下级菜单或按钮
        const category = this.findCategory(this.site_id, id];
        if (this.model.where([ [ 'cate_id', '=', id ) )).count() > 0)
            throw new AdminBusinessException('ATTACHMENT_GROUP_HAS_IMAGE'];

        //下级存在图片不能删除
        return category.delete();

    }

    /**
     * 管理端获取附件组列表
     * @param data
     * @return * @throws DbBusinessException
     */
    async getCategoryPage(data)
    {
        const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'type' ))) {
            where[] = [ 'type', '=', data[ 'type' ] ];
  }

  /**
   * getCategoryPage
   * 对应 PHP: AttachmentService_admin::getCategoryPage()
   * 逻辑类型: undefined - undefined
   */
  async getCategoryPage(data: any[]) {
    // 基于PHP真实逻辑: getCategoryPage
    // PHP原文: $where = array(             [ 'site_id', '=', $this->site_id ]         );         if (!empty($data[ 'type' ])) {             $where[] = [ 'type', '=',...
const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'type' ))) {
            where[] = [ 'type', '=', data[ 'type' ] ];
  }

  /**
   * getCategoryList
   * 对应 PHP: AttachmentService_admin::getCategoryList()
   * 逻辑类型: undefined - undefined
   */
  async getCategoryList(data: any[]) {
    // 基于PHP真实逻辑: getCategoryList
    // PHP原文: $where = array(             [ 'site_id', '=', $this->site_id ]         );         if (!empty($data[ 'type' ])) {             $where[] = [ 'type', '=',...
const where = [
            [ 'site_id', '=', this.site_id )
        ];
        if (!!data[ 'type' ))) {
            where[] = [ 'type', '=', data[ 'type' ] ];
  }
}
